package com.atguigu.stack;

/**
 * @author WangJX
 * @version 1.0
 * @date 2019/9/28 23:47
 */
public class LinkedListStackDemo {
    public static void main(String[] args) {
        LinkedListStack stack = new LinkedListStack();
        LinkedStack l1 = new LinkedStack("1");
        LinkedStack l2 = new LinkedStack("2");
        LinkedStack l3 = new LinkedStack("3");
        LinkedStack l4 = new LinkedStack("4");

        stack.push(l1);
        stack.push(l2);
        stack.push(l3);
        stack.push(l4);

        System.out.println("显示原本节点：");
        stack.show();

        System.out.println("取出一个栈数据：" + stack.pop());
        System.out.println("取出一个栈数据：" + stack.pop());

        System.out.println("显示剩余节点：");
        stack.show();

//        System.out.println(stack.listNum());
    }
}

class LinkedListStack{
    private LinkedStack top;

    public LinkedListStack() {
        this.top = new LinkedStack("");
    }


    /**
     * 判断栈是否为空
     * @return
     */
    public boolean isEmpty() {
        if (top.getPre() == null) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 插入数据
     * @param val
     */
    public void push(LinkedStack val) {
        //判断是否是第一次插入数据
        if (isEmpty()) {
            top.setPre(val);
        }else {
            LinkedStack temp = top.getPre();
            val.setPre(temp);
            top.setPre(val);
        }
    }

    /**
     * 显示栈内容
     */
    public void show() {
        if (isEmpty()) {
            throw new RuntimeException("栈为空");
        }
        LinkedStack temp = top;
        while (true) {
            if (temp.getPre() == null) {
                break;
            }
            System.out.println(temp.getPre());
            temp = temp.getPre();
        }
    }

    /**
     * 统计栈中的节点个数
     * @return
     */
    public int listNum() {
        if (isEmpty()) {
            throw new RuntimeException("栈为空");
        }
        int sum = 0;
        LinkedStack temp = top;
        while (true) {
            if (temp.getPre() == null) {
                break;
            }
            sum++;
            temp = temp.getPre();
        }
        return sum;
    }

    /**
     * 取出一个数据
     * @return
     */
    public LinkedStack pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈为空");
        }
        //获取top下第二个节点
        LinkedStack temp = top.getPre().getPre();
        LinkedStack value = top.getPre();
        top.setPre(temp);
        return value;
    }

}

class LinkedStack{
    private int id;
    private String value;
    private LinkedStack pre;

    public LinkedStack(String value) {
//        this.id = id;
        this.value = value;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public LinkedStack getPre() {
        return pre;
    }

    public void setPre(LinkedStack pre) {
        this.pre = pre;
    }

    @Override
    public String toString() {
        return "value=" + value ;
    }
}
